package com.cyms.modules.card.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.cyms.common.annotation.SysLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cyms.modules.card.entity.FuelcardEntity;
import com.cyms.modules.card.entity.FuelcardRechargeEntity;
import com.cyms.modules.card.service.FuelcardRechargeService;
import com.cyms.modules.card.service.FuelcardService;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.R;



/**
 * 油卡充值记录
 *
 * @author low
 * @email tolower@foxmail.com
 * @date 2018-12-08 14:54:32
 */
@RestController
@RequestMapping("card/fuelcardrecharge")
public class FuelcardRechargeController {
    @Autowired
    private FuelcardRechargeService fuelcardRechargeService;
    @Autowired
    private FuelcardService fuelcardService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = fuelcardRechargeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
			FuelcardRechargeEntity fuelcardRecharge = fuelcardRechargeService.selectById(id);

        return R.ok().put("fuelcardRecharge", fuelcardRecharge);
    }

    /**
     * 保存
     */
    @SysLog("新增加油卡充值信息")
    @RequestMapping("/save")
    public R save(@RequestBody FuelcardRechargeEntity fuelcardRecharge){
		fuelcardRecharge.setCreateDate(new Date());
		fuelcardRecharge.setRecordDate(new Date());
    	fuelcardRechargeService.insert(fuelcardRecharge);
    	FuelcardEntity fuelcard = fuelcardService.selectById(fuelcardRecharge.getCardId());
    	fuelcard.setTotalRecharge(fuelcard.getTotalRecharge().add(fuelcardRecharge.getMoney()));
    	fuelcard.setRestMoney(fuelcard.getRestMoney().add(fuelcardRecharge.getMoney()));
    	fuelcardService.updateById(fuelcard);
        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改加油卡充值信息")
    @RequestMapping("/update")
    public R update(@RequestBody FuelcardRechargeEntity fuelcardRecharge){
			fuelcardRechargeService.updateById(fuelcardRecharge);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除加油卡充值信息")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
			fuelcardRechargeService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
